package com.example.lenovo.testdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.example.lenovo.testdemo.ImageBean;
import com.example.lenovo.testdemo.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by Lenovo on 2017/11/21.
 */

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
    private Context mContext;
    private ArrayList<ImageBean> mDatas;
    private OnItemSelectListener onItemSelectListener;
    public MainAdapter(Context context , ArrayList<ImageBean> data){
        mContext = context;
        mDatas = data;
    }

    public interface OnItemSelectListener{
        void onItemSelect(ImageBean bean , boolean isCheck);
    }

    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener ){
        this.onItemSelectListener = onItemSelectListener;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  view = View.inflate(mContext , R.layout.item_main_adapter , null);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MainViewHolder holder, final int position) {
        final ImageBean bean = mDatas.get(position);
        Picasso.with(mContext).load(new File(mDatas.get(position).getFileName())).placeholder(R.mipmap.ic_launcher).resize(350 , 350).centerCrop().into(holder.img);
//        holder.img.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.ic_launcher));
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isCheck = holder.checkBox.isChecked();
                holder.checkBox.setChecked(!isCheck);
            }
        });

        //先把监听器设置为空，防止复用时候checkbox错位改变了状态
        holder.checkBox.setOnCheckedChangeListener(null);
        holder.checkBox.setChecked(bean.isIcSelect());
        //再设置一次CheckBox的选中监听器，当CheckBox的选中状态发生改变时，把改变后的状态储存在bean中
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(onItemSelectListener != null){
                    onItemSelectListener.onItemSelect(bean , isChecked);
                }
                bean.setIcSelect(isChecked);
            }
        });
//        Log.d("zouzhihao", "onBindViewHolder: " + bean.getFileName());
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class MainViewHolder extends RecyclerView.ViewHolder{
        private ImageView img;
        private CheckBox checkBox;
        public MainViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.iv_img);
            checkBox = itemView.findViewById(R.id.cb_check);
        }
    }
}
